package com.yx.common.core.utils;


import com.yx.common.core.exception.custom.MyException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Security;
import java.util.Base64;

public class AesUtil {

    /**
     * 密钥, 256位32个字节
     */
    public static final String DEFAULT_SECRET_KEY = "0aaadb59fbbf456bb57718a46bff2838";
    private static final String SECRET = "AES";
    private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    /**
     * AES加密ECB模式PKCS7Padding填充方式
     * @param str 字符串
     * @return 加密字符串
     * @throws Exception 异常信息
     */
    public static String encrypt(String str) {
        byte[] doFinal = null;
        try {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            byte[] keyBytes = DEFAULT_SECRET_KEY.getBytes(StandardCharsets.UTF_8);
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, SECRET));
            doFinal = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8));
            //return new String(Base64.getEncoder().encode(doFinal));
        }catch (Exception e){
            LoggerFactory.getLogger("加密失败");
            throw new MyException("AES加密失败");
        }
        return new String(Base64.getEncoder().encode(doFinal));
    }

    /**
     * AES解密ECB模式PKCS7Padding填充方式
     * @param str 字符串
     * @return 解密字符串
     * @throws Exception 异常信息
     */
    public static String decrypt(String str){
        byte[] doFinal = null;
        try {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            byte[] keyBytes = DEFAULT_SECRET_KEY.getBytes(StandardCharsets.UTF_8);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, SECRET));
            doFinal = cipher.doFinal(Base64.getDecoder().decode(str));
        }catch (Exception e){
            LoggerFactory.getLogger("解密失败");
            throw new MyException("AES解密失败");
        }
        return new String(doFinal);
    }

    public static void main(String[] args) throws Exception {
    /*    JSONObject json = new JSONObject();
        json.put("clintId","9f08520eaab74219911fc1b7ae84e946");
        json.put("code","SER00001224");
        json.put("random","de23ef036a0a33b22f12cf67896494d4");
        json.put("timestamp","1620704364362");

        json.put("id","123av");
        json.put("name","某某机构");
        json.put("endtime","2021-12-31");

        System.out.println(json.toString());
        String encryptStr = AesUtil.encrypt(json.toString());
        System.out.println("加密后字符串：" + encryptStr);
        //String decryptStr = AesUtil.decrypt(encryptStr);
        String decryptStr = AesUtil.decrypt("26FXpUeWMF3PFOT+mXxkpL3zOeb9HFUVRlw2TuJjt5mWu9XrTfiCMj4Aql5f5bvk9x/bM2pEKxujN2HwZu8+Bqs/B7dkT0Hrz9CR50/s8F4PS5CCIXkqVWSoQTz3Kln6av5DB4e2DV/2qqUG7MsL0ZV1RPf/M8WGHE0EZc48+H6ho+P0wqD6dMm3uBz9SHw9NxKrcv+xgEOCpZ96v/mBl+yhRGYc0uH1tLiCaL9CffScpCExxCOi0nHHtopgO8Ydg8A3Mq2Oaf6BBU5d33BWsMEM25X2V++JXxnGwYqoRfrnvtOL6WsgVH3ZE1zrxKKm1PIfyIKcNjrfRMR1Z1V834wJ2Pd9vjaxiTcn0SWRkorNDV/s/lpZjGTHojMa3ATJy0mcE6eAuimu8gKPjVRcyMQwkrr/62MyyV2smI/Rt1j50C5y8xbVp1PCnUQipUqrdt6ObEFEyBDPrZaX5rdJlqIiTNT2arii+mt4/7oeLbvF7TFvJdeMWfxUJZDL4tYazbE/WrAYDJ8kkptjeuPCaJvtbzJ7OTBYZ3oYHDe7nVmACpsaBV5Pz79ldBl1TqxJT/FH7xA4q8i7mNAVakrVa9BHKnyJ5PlbovEx1qOzLzZQ1wh1FpPYEQ6zvpjEMWDzGoSeTZao00ISIjVdrxohxeBSNbtwh/iiOv93e+YjvnUnd5iKVnVOM0IQTutcEUo48PO3NKnElObu2nvqF5e22ttB6mM4fMxvvCEEFSswnhi8urZxwlCMsLfxvCWmIFWWfoQsh8btcDE/mCN+i6Gik73Mgs8sfjIdC+DXva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ean flag = Common.isnull(decryptStr);
        System.out.println("解密后字符串：" + decryptStr);

        String a = AesUtil.decrypt("M86Qv/dlCAeo0cY/WqkNJQ==");
        System.out.println("解密后字符串：" + a);
*/
        //String encrypt = AesUtil.encrypt("123456");
        //System.out.println(encrypt);



    }

}
